package com.cpi.cornpp.instructionspurchases.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.instructionspurchaseforecast.dto.InstructionPurchaseForecastDTO;
import com.cpi.cornpp.instructionspurchaseforecast.dto.InstructionPurchaseForecastSearchDTO;
import com.cpi.cornpp.instructionspurchaseforecast.view.InstructionPurchaseForecastSearchView;
import com.cpi.cornpp.instructionspurchaseforecast.view.InstructionPurchaseForecastView;
import com.cpi.cornpp.instructionspurchases.dto.InstructionPurchaseDTO;
import com.cpi.cornpp.instructionspurchases.dto.InstructionPurchaseSearchDTO;
import com.cpi.cornpp.instructionspurchases.exception.InstructionPurchaseException;
import com.cpi.cornpp.instructionspurchases.view.InstructionPurchaseCreateView;
import com.cpi.cornpp.instructionspurchases.view.InstructionPurchaseSearchView;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;
import com.cs.common.utils.NumberUtil;
import com.cs.common.utils.StringUtils;
import com.cs.common.utils.TimeUtils;

public class InstructionPurchaseConverter {
	
	private static final char ZERO = '0';
	private static final String INSTRUCTION_PURCHASE_REFERENCE_LENGTH_CODE = "INSTRUCTION_PURCHASE_REFERENCE_LENGTH_CODE";
	private static final String INSTRUCTION_PURCHASE_DETAIL_REFERENCE_LENGTH_CODE = "INSTRUCTION_PURCHASE_DETAIL_REFERENCE_LENGTH_CODE";
	
	public List convertDTOsToCreateViews(List instructionPurchaseDTOs)throws InstructionPurchaseException{
		List instructionPurchaseCreateViews = null;
		Iterator iterator = null;
		InstructionPurchaseDTO instructionPurchaseDTO = null;
		InstructionPurchaseCreateView instructionPurchaseCreateView = null;
		InstructionPurchaseCreateView instructionPurchaseCreateAntView = null;
		try{
			instructionPurchaseCreateViews = new ArrayList(0);
			iterator = instructionPurchaseDTOs.iterator();
			while(iterator.hasNext()){
				instructionPurchaseDTO = (InstructionPurchaseDTO)iterator.next();
				instructionPurchaseCreateView = convertDTOToCreateView(instructionPurchaseDTO);
				if( instructionPurchaseCreateAntView != null ){
					if(instructionPurchaseCreateAntView.getReference().equals(instructionPurchaseCreateView.getReference())){
						instructionPurchaseCreateView.setId(0);
					}
				}
				instructionPurchaseCreateViews.add(instructionPurchaseCreateView);
				instructionPurchaseCreateAntView = instructionPurchaseCreateView;
			}
		}catch( InstructionPurchaseException instructionPurchaseException ){
			throw instructionPurchaseException;
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException = new InstructionPurchaseException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}
		return instructionPurchaseCreateViews;
	}
	
	public InstructionPurchaseCreateView convertDTOToCreateView(InstructionPurchaseDTO instructionPurchaseDTO)throws InstructionPurchaseException{
		InstructionPurchaseCreateView instructionPurchaseCreateView = null;
		SystemParameterCollection systemParameterCollection = null;
		SystemParameterBean systemParameterBean = null;
		try{
			systemParameterCollection = SystemParameterCollection.getInstance();
			systemParameterBean = systemParameterCollection.getSystemParameterBean(INSTRUCTION_PURCHASE_REFERENCE_LENGTH_CODE);
			instructionPurchaseCreateView = new InstructionPurchaseCreateView();
			instructionPurchaseCreateView.setId(instructionPurchaseDTO.getId());
			instructionPurchaseCreateView.setReference(NumberUtil.fill(instructionPurchaseDTO.getId(), ZERO, systemParameterBean.getIntValue()));
			instructionPurchaseCreateView.setDate(TimeUtils.convertSQLDateToString(instructionPurchaseDTO.getDate()));
			instructionPurchaseCreateView.setComments(instructionPurchaseDTO.getComments());
			instructionPurchaseCreateView.setPurchaseStatus(instructionPurchaseDTO.getPurchaseStatus());
			instructionPurchaseCreateView.setItemCount(instructionPurchaseDTO.getItemCount());
			instructionPurchaseCreateView.setPurchaseDetailId(instructionPurchaseDTO.getPurchaseDetailId());
			systemParameterBean = systemParameterCollection.getSystemParameterBean(INSTRUCTION_PURCHASE_DETAIL_REFERENCE_LENGTH_CODE);
			if(instructionPurchaseDTO.getRow()>0){
				instructionPurchaseCreateView.setRowNumber(NumberUtil.fill(instructionPurchaseDTO.getRow(), ZERO, systemParameterBean.getIntValue()));
			}else{
				instructionPurchaseCreateView.setRowNumber(StringUtils.EMPTY_STRING);
			}
			instructionPurchaseCreateView.setCustomerId(instructionPurchaseDTO.getCustomerId());
			if( instructionPurchaseDTO.getCustomerName() != null ){
				instructionPurchaseCreateView.setCustomerName(instructionPurchaseDTO.getCustomerName().length() > 28 ? instructionPurchaseDTO.getCustomerName().substring(0,25) + "..." : instructionPurchaseDTO.getCustomerName() );
			}else{
				instructionPurchaseCreateView.setCustomerName(StringUtils.EMPTY_STRING);
			}
			instructionPurchaseCreateView.setItemQuantity(NumberUtil.convertQuantity(instructionPurchaseDTO.getItemQuantity()));
			instructionPurchaseCreateView.setCornQuantity(NumberUtil.convertQuantity(instructionPurchaseDTO.getCornQuantity()));
			instructionPurchaseCreateView.setContractQuantity(NumberUtil.convertQuantity(instructionPurchaseDTO.getContractQuantity()));
			instructionPurchaseCreateView.setContractQuantityPurchased(NumberUtil.convertQuantity(instructionPurchaseDTO.getContractQuantityPurchased()));
			instructionPurchaseCreateView.setContractBalance(NumberUtil.convertQuantity(instructionPurchaseDTO.getContractBalance()));
			instructionPurchaseCreateView.setFuturesType(instructionPurchaseDTO.getFuturesType());
			instructionPurchaseCreateView.setStratety(instructionPurchaseDTO.getStrategy());
			instructionPurchaseCreateView.setType(instructionPurchaseDTO.getType());
			instructionPurchaseCreateView.setPurchaseDetailStatus(instructionPurchaseDTO.getPurchaseDetailStatus());
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException = new InstructionPurchaseException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}
		return instructionPurchaseCreateView;
	}
	
	

	public InstructionPurchaseSearchDTO convertViewToSearchDTO(InstructionPurchaseSearchView instructionPurchaseSearchView)throws InstructionPurchaseException{
		InstructionPurchaseSearchDTO instructionPurchaseSearchDTO = null;
		StringBuffer balance = null;
		int selectBalance = 0;
		try{
			instructionPurchaseSearchDTO = new InstructionPurchaseSearchDTO();
			instructionPurchaseSearchDTO.setReference(instructionPurchaseSearchView.getReference());
			instructionPurchaseSearchDTO.setCustomer(instructionPurchaseSearchView.getCustomerId());
			instructionPurchaseSearchDTO.setItem(instructionPurchaseSearchView.getItemId());
			instructionPurchaseSearchDTO.setPurchaseStatus(instructionPurchaseSearchView.getPurchaseStatus());
			instructionPurchaseSearchDTO.setPurchaseDetailStatus(instructionPurchaseSearchView.getPurchaseDetailStatus());
			if( instructionPurchaseSearchView.getBalance() != null && !instructionPurchaseSearchView.getBalance().trim().equals(StringUtils.EMPTY_STRING) ){
				balance = new StringBuffer(0);
				selectBalance = NumberUtil.parseInt(instructionPurchaseSearchView.getSelectBalance());
				switch(selectBalance){
					case 1: balance.append(StringUtils.MAJOR_CHAR);
					break;
					case 2: balance.append(StringUtils.MINOR_CHAR);
					break;
					case 3: balance.append(StringUtils.EQUALS_CHAR);
					break;
				}
				balance.append(instructionPurchaseSearchView.getBalance());
				instructionPurchaseSearchDTO.setBalance(balance.toString());
			}
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException = new InstructionPurchaseException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}
		
		return instructionPurchaseSearchDTO;
	}

	/**
	 * Metodo que convierte Vistas a DTOs
	 * 
	 * @author Edgar Joao
	 * @param InstructionPurchaseCreateView
	 * @return InstructionPurchaseDTO
	 * @throws InstructionPurchaseException
	 */
	public InstructionPurchaseDTO convertViewToDTO(InstructionPurchaseCreateView view) throws InstructionPurchaseException{
		InstructionPurchaseDTO dto = null;
		try{
			if(view != null){
				dto = new InstructionPurchaseDTO();
				dto.setId(view.getId());
				dto.setComments(view.getComments());
				dto.setPurchaseStatus(view.getPurchaseStatus());
				dto.setDate(TimeUtils.convertStringToSQLDate(view.getDate()));
			}
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException = new InstructionPurchaseException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}
		return dto;
	}
	/**
	 * 
	 * @param searchView
	 * @return
	 * @throws InstructionPurchaseException
	 */
	public InstructionPurchaseForecastSearchDTO convertViewToSearchDTO(InstructionPurchaseForecastSearchView searchView)throws InstructionPurchaseException{
		InstructionPurchaseForecastSearchDTO searchDTO = null;
		double coverage = 0;
		try{
			searchDTO = new InstructionPurchaseForecastSearchDTO();			
			searchDTO.setCustomerDescription(searchView.getCustomerDescription());			
			searchDTO.setItemDescription(searchView.getItemDescription());
			if(!org.apache.commons.lang.StringUtils.isEmpty(searchView.getCoverage())){
				coverage = Double.parseDouble(searchView.getCoverage());
				if(searchView.getSelectCoverage().equals(StringUtils.EQUALS_CHAR)){
					searchDTO.setCoverageInit(coverage/100);
					searchDTO.setCoverageEnd(coverage/100);
				}else if(searchView.getSelectCoverage().equals(StringUtils.MAJOR_CHAR)){
					searchDTO.setCoverageInit(coverage/100);
					searchDTO.setCoverageEnd(1);
				}else if(searchView.getSelectCoverage().equals(StringUtils.MINOR_CHAR)){
					searchDTO.setCoverageInit(0);
					searchDTO.setCoverageEnd(coverage/100);
				}
			}else{
				searchDTO.setCoverageInit(0);
				searchDTO.setCoverageEnd(1);
			}
			searchDTO.setFromYear(NumberUtil.parseInt(searchView.getFromYear()));
			if(searchView.getToYear().equals("-1")){
				searchDTO.setToYear(NumberUtil.parseInt(searchView.getFromYear()));
			}else{			
				searchDTO.setToYear(NumberUtil.parseInt(searchView.getToYear()));
			}
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException = new InstructionPurchaseException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}
		
		return searchDTO;
	}
	
	
	public List convertDTOsToForecastViews(List instructionPurchaseForecastDTOs)throws InstructionPurchaseException{
		List instructionPurchaseForecastViews = null;
		Iterator iterator = null;
		InstructionPurchaseForecastDTO instructionPurchaseForecastDTO = null;
		InstructionPurchaseForecastView instructionPurchaseForecastView = null;
		InstructionPurchaseForecastView instructionPurchaseForecastViewTotal = null;
		double contrated = 0;
		double nonContacted = 0;
		double cornContracted = 0;
		int futures = 0;
		try{
			instructionPurchaseForecastViews = new ArrayList(0);
			iterator = instructionPurchaseForecastDTOs.iterator();
			instructionPurchaseForecastViewTotal = new InstructionPurchaseForecastView();
			while(iterator.hasNext()){
				instructionPurchaseForecastDTO = (InstructionPurchaseForecastDTO)iterator.next();
				instructionPurchaseForecastView = convertDTOToForecastView(instructionPurchaseForecastDTO);
				contrated += instructionPurchaseForecastDTO.getContracted();
				nonContacted += instructionPurchaseForecastDTO.getNonContracted();
				cornContracted += instructionPurchaseForecastDTO.getCornContracted();
				futures += instructionPurchaseForecastDTO.getInstructionFutures();
				instructionPurchaseForecastViews.add(instructionPurchaseForecastView);
			}
			instructionPurchaseForecastViewTotal.setTotal(NumberUtil.convertQuantity(contrated+nonContacted));
			instructionPurchaseForecastViewTotal.setContracted(NumberUtil.convertQuantity(contrated));
			instructionPurchaseForecastViewTotal.setNonContracted(NumberUtil.convertQuantity(nonContacted));
			instructionPurchaseForecastViewTotal.setCornContracted(NumberUtil.convertQuantity(cornContracted));
			instructionPurchaseForecastViewTotal.setInstructionFutures(NumberUtil.convertInteger(futures));
			instructionPurchaseForecastViews.add(instructionPurchaseForecastViewTotal);
		}catch( InstructionPurchaseException instructionPurchaseException ){
			throw instructionPurchaseException;
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException = new InstructionPurchaseException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}
		return instructionPurchaseForecastViews;
	}
	
	/**
	 * 
	 * @param dto
	 * @return
	 * @throws InstructionPurchaseException
	 */
	public InstructionPurchaseForecastView convertDTOToForecastView(InstructionPurchaseForecastDTO dto)throws InstructionPurchaseException{
		InstructionPurchaseForecastView instructionPurchaseForecastView = null;		
		try{			
			instructionPurchaseForecastView = new InstructionPurchaseForecastView();
			instructionPurchaseForecastView.setYear(dto.getYear());
			instructionPurchaseForecastView.setMonth(StringUtils.getMonth(dto.getMonth()));
			instructionPurchaseForecastView.setCustomerId(dto.getCustomerId().trim());
			instructionPurchaseForecastView.setCustomerDescription(dto.getCustomerDescription().length()>28?dto.getCustomerDescription().substring(0,25)+"...":dto.getCustomerDescription());
			instructionPurchaseForecastView.setItemId(dto.getItemId().trim());
			instructionPurchaseForecastView.setItemDescription(dto.getItemDescription().length()>28?dto.getItemDescription().substring(0,25)+"...":dto.getItemDescription());
			instructionPurchaseForecastView.setContracted(NumberUtil.convertQuantity(dto.getContracted()));
			instructionPurchaseForecastView.setNonContracted(NumberUtil.convertQuantity(dto.getNonContracted()));
			instructionPurchaseForecastView.setCornContracted(NumberUtil.convertQuantity(dto.getCornContracted()));
			instructionPurchaseForecastView.setNonCornContracted(NumberUtil.convertQuantity(dto.getNonCornContracted()));
			instructionPurchaseForecastView.setInstructionFutures(NumberUtil.convertInteger(dto.getInstructionFutures()));
			instructionPurchaseForecastView.setTotal(NumberUtil.convertQuantity((dto.getContracted() + dto.getNonContracted())));
			instructionPurchaseForecastView.setCoverage(NumberUtil.convertQuantity(dto.getCoverage()));
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException = new InstructionPurchaseException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}
		return instructionPurchaseForecastView;
	}

	/**
	 * Metodo que convierte DTOs a Vistas
	 * 
	 * @author Edgar Joao
	 * @param dto InstructionPurchaseDTO
	 * @return view InstructionPurchaseCreateView
	 * @throws InstructionPurchaseException
	 */
	public InstructionPurchaseCreateView convertDTOToView(InstructionPurchaseDTO dto) throws InstructionPurchaseException{
		InstructionPurchaseCreateView view = null;
		SystemParameterCollection systemParameterCollection = null;
		SystemParameterBean systemParameterBean = null;
		try{
			systemParameterCollection = SystemParameterCollection.getInstance();
			systemParameterBean = systemParameterCollection.getSystemParameterBean(INSTRUCTION_PURCHASE_REFERENCE_LENGTH_CODE);
			if(dto != null){
				view = new InstructionPurchaseCreateView();
				view.setReference(NumberUtil.fill(dto.getId(), '0', systemParameterBean.getIntValue()));
				view.setId(dto.getId());
				view.setComments(dto.getComments());
				view.setPurchaseStatus(dto.getPurchaseStatus());
				view.setDate(TimeUtils.convertSQLDateToString(dto.getDate()));
			}
		}catch( Exception exception ){
			InstructionPurchaseException instructionPurchaseException = null;
			exception.printStackTrace(System.out);
			instructionPurchaseException = new InstructionPurchaseException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw instructionPurchaseException;
		}
		return view;
	}
}
